#include <myheader.h>

/* Usage:  */
int main(int argc, char *argv[]){                                  
   //打开目录流，判断参数数量是否正确
    ARGS_CHECK(argc,2);
    DIR*dirp=opendir(argv[1]);
    ERROR_CHECK(dirp,NULL,"opendir");
    //读取目录项
    struct dirent*dent;
    dent=readdir(dirp);
    int num=0;
    off_t  total_size=0;
    while(dent!=NULL){
        //先找到几个1.txt文件
        if(strcmp(dent->d_name,"1.txt")==0){
        num++;
        //创建一个存储路径的字符数组，拼接成绝对路径，方便获取文件的大小
        char full_path[1024]={0};
        snprintf(full_path,sizeof(full_path),"%s/%s",argv[1],dent->d_name);
       //先定义一个结构体对象，用来存储读到的每一个文件信息
        struct stat file_stat;
        //保险措施，确定读到了
        if(stat(full_path,&file_stat)==-1){
            continue;
        }
        //把字节大小给加起来
        total_size+=file_stat.st_size;
        printf("找到1.txt%s,大小%ld字节\n",full_path,file_stat.st_size);
        }
        dent=readdir(dirp);

    }

printf("\n目录%s中共有%d个1.txt文件，总大小%ld字节\n",argv[1],num,total_size);

    closedir(dirp);
    return 0;
}

